package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author Kyl
	 */
	public class Ball extends FlxSprite 
	{
		
		[Embed (source = 'ball.png')]
		private var ballArt:Class;
		private var dead:Boolean;
		
		public function Ball(X:Number=0, Y:Number=0, R:Number=10) 
		{
			super(X, Y);
			acceleration.y = 100;
			width = R;
			height = R;
			drag = new FlxPoint(10, 10);
			elasticity = .75;
			mass = 0.50;
			dead = false;
			
			radius = R;
			
			loadGraphic(ballArt, false, false, R, R, false);
			//makeGraphic(R, R, 0xFF000000);
			
			addAnimation("death", [0, 1, 2, 3], 10, false);
		}
		
		override public function update():void 
		{
			super.update();

			if (dead && finished) {
				kill();
			}
		}
		
		public function death():void
		{
			play("death");
			velocity.x = 0;
			velocity.y = 0;
			acceleration.y = 0;
			dead = true;
		}
		
		
		private var radius:Number;
		
		public function getRadius() : Number { return radius; }
		
		// Bounce off of an ink object using the given normal vector
		public function inkBounce(normalX:Number, normalY:Number, 
								  pointX:Number, pointY:Number) : void
		{
			// First normalize our velocity and the normal
			var normPt:FlxPoint = new FlxPoint(normalX, normalY);
			var origin:FlxPoint = new FlxPoint(0, 0);
			var normLen:Number = FlxU.getDistance(normPt, origin);
			normPt.x /= normLen;
			normPt.y /= normLen;
			
			// Use the equation for reflection about a surface normal (close enough 
			// for our purposes: d_s = 2(d_n*d_i)d_n - d_i.
			// Where: d_s is a unit vector indicating direction of reflection
			// d_n is the surface normal (pointing away from the surface)
			// d_i is a vector from the point of contact to the source (-velocity)
			
			var velocityLength:Number = FlxU.getDistance(velocity, origin);
			var d_i:FlxPoint = new FlxPoint(velocity.x, velocity.y);
			
			d_i.x /= velocityLength;
			d_i.y /= velocityLength;
			
			// We need to make sure to use the proper normal.  The angle between the normal and
			// d_i must be < 90 deg.  Since A*B = cos(T) for unit vectors, we assert that
			// d_i * d_n >= 0.
			// The normal must point in the same direction as a vector from the point of
			// contact to the sprite's center
			
			var cosTheta:Number = d_i.x * normPt.x + d_i.y * normPt.y;
			
			if ( cosTheta <= 0 )
			{	
				// We're using the "wrong" surface normal
				normPt.x = -1 * normPt.x; normPt.y = -1 * normPt.y;
				normPt.x /= FlxU.getDistance(normPt, origin);
				normPt.y /= FlxU.getDistance(normPt, origin);
				
				cosTheta = d_i.x * normPt.x + d_i.y * normPt.y;
			}
			
			// Since cosTheta = d_n dot d_i...
			var d_sX:Number = d_i.x - ( 2 * normPt.x * cosTheta );
			var d_sY:Number = d_i.y - ( 2 * normPt.y * cosTheta );
			// Make sure we're not still overlapping the ink.
			
			// Finally, we scale velocity back up and multiply by our collision elasticity
			velocity.x = d_sX * velocityLength * elasticity;
			velocity.y = d_sY * velocityLength * elasticity;
			x = pointX - (normPt.x * 15);
			y = pointY - (normPt.y * 15);
		}
		
	}

}